package com.lytoyo.neihanduanzi.utils;

import java.io.File;
import java.io.IOException;

public class FfmpegUtil {
    public static void extractFirstFrame(File videoFile, File outputImage) throws IOException, InterruptedException {
        ProcessBuilder pb = new ProcessBuilder(
                "ffmpeg",
                "-i", videoFile.getAbsolutePath(),
                "-vframes", "1",          // 截取1帧
                "-q:v", "2",              // 图像质量（1-31，越小质量越高）
                outputImage.getAbsolutePath()
        );

        // 合并错误流到标准输出（便于调试）
        pb.redirectErrorStream(true);

        Process process = pb.start();
        int exitCode = process.waitFor();

        if (exitCode != 0) {
            throw new IOException("FFmpeg 执行失败，退出码: " + exitCode);
        }
    }
}